/**
 * Copyright 2009 Jesse Brown (jbrown@fluentv.com)
 * 
 * Licensed under the Apache License, Version 2.0 (the "License"); 
 * you may not use this file except in compliance with the License. 
 * You may obtain a copy of the License at 
 *   http://www.apache.org/licenses/LICENSE-2.0 
 * 
 * Unless required by applicable law or agreed to in writing, software 
 * distributed under the License is distributed on an "AS IS" BASIS, 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
 * See the License for the specific language governing permissions and 
 * limitations under the License. 
 */
package com.fluentv.games.netblocks;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.Window;

/**
 * Activity that encapsulates the actual game play.
 * 
 * @author Jesse Brown (jbrown@fluentv.com)
 */
public class PlayingActivity extends Activity {
  
  public static final String GAME_ID_KEY = "GameId";
  public static final String MUTED_KEY = "Muted";
  
  private static final String LOG_TAG = "PlayingActivity";
    
  private PlayingView playingView;
  private SoundEffectsPlayer soundEffects;
    
  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    
    // No window title (just burns space we could be using)
    requestWindowFeature(Window.FEATURE_NO_TITLE);
               
    soundEffects = new SoundEffectsPlayer(this);
    playingView = new PlayingView(this, soundEffects, 15);          
    setContentView(playingView);
       
    // Do not actually start playing or load state. This will be done as necessary
    // when the state is restored.
  }
  
  // Determine if we should resume or start over again.
  @Override
  protected void onNewIntent(Intent intent) {
    setIntent(intent);
  }

  @Override
  protected void onPause() {
    super.onPause();
    
    Log.v(LOG_TAG, "Stopped piece movement (we have paused)");
    playingView.endPieceMovement();

    // Flush out to our preferences so everything can be restored when
    // resuming the game.
    Editor editor = getPreferences(MODE_PRIVATE).edit();;
    playingView.saveState(editor);
    
    // Flush our game ID.
    editor.putString(GAME_ID_KEY, getIntent().getStringExtra(GAME_ID_KEY));
    editor.commit();
  }
  
  @Override
  protected void onResume() {
    super.onResume();
    
    // See what the current intent is, and reset the game (and don't load state)
    // if we should be loading a new view, or load the existing state if we are
    // resuming.
    Intent intent = getIntent();
    Log.v(LOG_TAG, "Resuming. Current intent is " + intent);

    // Get the sound status (muted = true/false) and update the playingView. 
    if (intent.getBooleanExtra(MUTED_KEY, false)) {
      soundEffects.mute();
    } else {
      soundEffects.unMute();
    }
    
    // Get the game ID. If it is set and does NOT match our current value, then
    // nuke the current state and reset.
    String gameId = intent.getStringExtra(GAME_ID_KEY);
    
    if (gameId != null && gameId.length() > 0) {
      // See if it matches our existing value. If not, reset everything.
      if (!gameId.equals(getPreferences(MODE_PRIVATE).getString(GAME_ID_KEY, ""))) {
        Log.v(LOG_TAG, "New Game requested, state reset.");
        playingView.resetGame();
        return;
      }
    } 
    
    Log.i(LOG_TAG, "Resume game requested, restoring previous game.");
    playingView.restoreState(getPreferences(MODE_PRIVATE));
  }

  @Override
  protected void onDestroy() {
    if (playingView != null) {
      playingView.destroy();
    }    
    super.onDestroy();
  }

  /**
   * Make sure we pause and resume the game if we lose or regain focus.
   */
  @Override
  public void onWindowFocusChanged(boolean hasFocus) {
    if (hasFocus) {
      Log.v(LOG_TAG, "Restarted piece movement (now have focus)");
      playingView.startPieceMovement();      
    } else {
      Log.v(LOG_TAG, "Ended piece movement (lost focus)");
      playingView.endPieceMovement();
    }
  } 
}
